package com.example.avmanagerweb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.avmanagerweb.Utils.String.StringUtils;
import com.example.avmanagerweb.Utils.String.StringValidator;
import com.example.avmanagerweb.entity.Staffdepartment;
import com.example.avmanagerweb.entity.SysDepartment;
import com.example.avmanagerweb.entity.SysUserinfo;
import com.example.avmanagerweb.redisService.RedisService;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.StaffdepartmentService;
import com.example.avmanagerweb.service.SysDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-06-26
 */
@RestController
    @RequestMapping("/staffdepartment")
public class StaffdepartmentController {
    @Autowired
    private StaffdepartmentService staffdepartmentService;

    @Autowired
    private SysDepartmentService sysDepartmentService;

    @Autowired
    private RedisService redisService;

//    1.	（一级二级）部门增删1.	（一级二级）部门增删改查接口改查接口

    @GetMapping(value = "/getDep")
    public RLIST getDep(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);
        Integer id = sysUserinfo.getUdepid();
        System.out.println(id);
        if(id== null ||id <= 0){
           return RLIST.error().message("异常 账号无部门id");
        }
        List<Staffdepartment> departmentList = staffdepartmentService.getDepartmentList(id);
        for (Staffdepartment staffdepartment : departmentList) {
            System.out.println(staffdepartment);
        }
        return RLIST.ok().data("data",departmentList);
    }

    @PostMapping(value = "/alterDep")
    public RLIST alterDep(@RequestBody SysDepartment sysDepartment, HttpServletRequest request){
        if(sysDepartment.getDepid() <= 0){
            return RLIST.error().message("修改组id异常");
        }
        if(StringUtils.isEmpty(sysDepartment.getDepname())){
            return RLIST.error().message("修改名称异常");
        }
        if(sysDepartment.getDepshoword() != null && sysDepartment.getDepshoword() < 0){
            return RLIST.error().message("显示排序 参数异常");
        }
        if(sysDepartment.getDepsavedays() != null && sysDepartment.getDepsavedays() < 0){
            return RLIST.error().message(" 整理日期天数 参数异常");
        }

        boolean validString = StringValidator.isValidStringNanem(sysDepartment.getDepname());
        if(!validString){
            return RLIST.error().message("名称长度2-12,不能使用符合 持数字和字母-汉字");
        }


        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }


        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);
        Integer id = sysUserinfo.getUdepid();
        if(id== null ||id <= 0){
            return RLIST.error().message("异常 账号无部门id");
        }




        List<SysDepartment> departmentList = sysDepartmentService.getSubquery(id);
        if(departmentList == null || departmentList.size() <=0){
            return RLIST.error().message("无部门");
        }
        boolean fla = true;
        for (SysDepartment department : departmentList) {
            System.out.println(department.getDepid() + "  ===  " + sysDepartment.getDepid());
            if(department.getDepid() == sysDepartment.getDepid() ){
                System.out.println( " 完成 ");
                fla = false ;
                break;
            }
        }
        System.out.println(fla);
        if(fla){
            return RLIST.error().message("无法跨部门修改");
        }

        QueryWrapper  wrapper = new QueryWrapper();
        wrapper.eq("DepartName",sysDepartment.getDepname());
        Staffdepartment one = staffdepartmentService.getOne(wrapper);
        if(one != null) {
            return RLIST.error().message("存在相同部门名称");
        }
        boolean b = sysDepartmentService.updateById(sysDepartment);
        if(b){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    @PostMapping(value = "/addDep")
    public RLIST addDep(@RequestBody SysDepartment sysDepartment){

        if(sysDepartment.getDeppid() == null || sysDepartment.getDeppid() <= 0) {
            return RLIST.error().message("父组ID为空");
        }

        if(StringUtils.isEmpty(sysDepartment.getDepname())){
            return RLIST.error().message("部门名称为空");
        }
        boolean validString = StringValidator.isValidStringNanem(sysDepartment.getDepname());
        if(!validString){
            return RLIST.error().message("名称长度2-12,不能使用符合 持数字和字母-汉字");
        }

        Staffdepartment staff = staffdepartmentService.getById(sysDepartment.getDeppid());
        if(staff == null) {
            return RLIST.error().message("父组不存在");
        }

        QueryWrapper  wrapper = new QueryWrapper();
        wrapper.eq("DepartName",sysDepartment.getDepname());
        Staffdepartment one = staffdepartmentService.getOne(wrapper);
        if(one != null) {
            return RLIST.error().message("存在相同部门名称");
        }



        sysDepartment.setDeplayer(staff.getDepartlayer()+1);
        if(sysDepartment.getDepsavedays() == null){
            sysDepartment.setDepsavedays(30);
        }
        if(sysDepartment.getDepshoword() == null){
            sysDepartment.setDepshoword(0);
        }

        sysDepartment.setDepisdel(false);
        if(sysDepartment.getDepremark() == null){
            sysDepartment.setDepremark("");
        }
        System.out.println(sysDepartment.toString());
        boolean b = sysDepartmentService.save(sysDepartment);
        if(b){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    @PostMapping(value = "/delDep")
    public RLIST delDep(@RequestParam("id") int id, @RequestParam("pid") int pid, HttpServletRequest request){
        if(id <= 0){
            return RLIST.error().message("删除组ID异常");
        }

        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        System.out.println(value);
        if( value == null || value == 0 ) {
            return RLIST.error().message("token异常 出现bug");
        }
        String  json = (String) redisService.getValue(value+"");
        JSONObject jsonObject = JSONObject.parseObject(json);
        SysUserinfo sysUserinfo = JSON.toJavaObject(jsonObject, SysUserinfo.class);
        Integer depid = sysUserinfo.getUdepid();
        if(depid == null || depid <= 0){
            return RLIST.error().message("异常 账号无部门id");
        }

        List<SysDepartment> departmentList = sysDepartmentService.getSubquery(depid);
        System.out.println(departmentList);
        if(departmentList == null || departmentList.size() <=0){
            return RLIST.error().message("无部门");
        }
        boolean fla = true;
        for (SysDepartment department : departmentList) {
            System.out.println(department.getDepid() + "   "+ id);
            if(department.getDepid() == id ){
                fla = false ;
                break;
            }
        }
        if(fla){
            return RLIST.error().message("无法跨部门删除");
        }


        Staffdepartment sd = staffdepartmentService.getById(id);
        if(sd == null || sd.getParentid() != pid){
            return RLIST.error().message("组不存在或者父组id不匹配");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("deppid",id);
        int count = sysDepartmentService.count(wrapper);
        if(count > 0){
            return RLIST.error().message("存在子组无法删除");
        }


        SysDepartment sysDepartment = new SysDepartment();
        sysDepartment.setDepid(id);
        sysDepartment.setDepisdel(true);
        boolean b = sysDepartmentService.updateById(sysDepartment);
        if(b){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

}

